﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Solution.Business;

namespace NewsMvc.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        ArticleBLL Repository = new ArticleBLL();
        public ActionResult Index(int? id)
        {
            try
            {
                int _ID = id ?? 1;
                int IDSkip = _ID - 1;
                int PageSize = 5;
                var LsArticle = Repository.GetAll().Where(t => t.flag != "D").OrderByDescending(p => p.Created_Date).Skip(IDSkip * PageSize).Take(PageSize);
                int _count = Repository.GetAll().Where(t => t.flag != "D").Count();
                int numPage = (int)Math.Ceiling((float)_count / (float)PageSize);
                string strPage = "";
                strPage += " <div class='paging'> ";
                int min = (_ID - 2) > 1 ? _ID - 2 : 1;
                int max = (_ID + 2) < numPage ? (_ID + 2) : numPage;
                if (_ID > 1)
                {
                    strPage += " <span class='page'><a href='" + Url.Content("~/Page/") + 1 + "'>«</a></span>";
                    strPage += " <span class='page'><a href='" + Url.Content("~/Page/") + (_ID - 1) + "'>‹</a></span> ... ";
                }
                for (int i = min; i <= max; i++)
                {
                    if (i == _ID)
                        strPage += " <span class='current_page'><a href='" + Url.Content("~/Page/") + i + "'>" + i + "</a></span>";
                    else
                        strPage += " <span class='page'><a href='" + Url.Content("~/Page/") + i + "'>" + i + "</a></span>";
                }
                if (_ID < numPage)
                {
                    strPage += " ... <span class='page'><a href='" + Url.Content("~/Page/") + (_ID + 1) + "'>›</a></span> ";
                    strPage += " <span class='page'><a href='" + Url.Content("~/Page/") + numPage + "'>»</a></span> ";
                }
                strPage += " </div> ";
                ViewBag.page = strPage;
                return View(LsArticle);
            }
            catch
            {
                return Redirect("~/home/Err/");
            }
        }
        public ActionResult Slide(int id)
        {
            return PartialView();
        }

        public ActionResult Err()
        {
            return View();
        }
    }
}
